#include "myproxy.h"

int recvn(int socket_descriptor, void* buf, int buf_len) {
	int n_left = buf_len;
	int n = 0;
	while (n_left > 0) {
		if ((n = recv(socket_descriptor, buf + (buf_len - n_left), n_left, 0)) < 0) {
			if (errno == EINTR)
				n = 0;
			else
				return -1;
		} else if (n == 0) {
			return 0;
		}
		n_left -= n;
	}
	return buf_len;
}

int sendn(int socket_descriptor, const void* buf, int buf_len) {
	int n_left = buf_len;
	int n;
	
	while (n_left > 0) {
		n = send(socket_descriptor, buf + (buf_len - n_left), n_left, MSG_NOSIGNAL);
		if (n < 0) {
		
			if (errno == EINTR){
				n = 0;
			}
			else {
				return -1;
			}
		} else if (n == 0) {
			return 0;
		}
		n_left -= n;
	}
	return buf_len;
}
